package com.fessc.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fessc.develop.entity.dbvo.TOrgUrlManage;
import com.fessc.develop.mapper.TOrgUrlManageMapper;
import com.fessc.develop.service.interfaces.ITOrgUrlManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.ExceptionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2024-07-02
 */
@Service
public class TOrgUrlManageServiceImpl extends ServiceImpl<TOrgUrlManageMapper, TOrgUrlManage> implements ITOrgUrlManageService {

    Map<String, Map<String, TOrgUrlManage>> OrgUrlMap;

    public final static String DEFAULT_INTERFACE_KEY = "COMMON";

    @PostConstruct
    void init() {
        List<TOrgUrlManage> orgUrlManageList = this.list();
        if (BusiUtil.isNull(orgUrlManageList)) {
            OrgUrlMap = new HashMap<>();
        } else {
            OrgUrlMap = orgUrlManageList.stream().collect(Collectors.toMap(item -> item.getOrgCode() + item.getOrgType(),
                    p -> {
                        Map<String, TOrgUrlManage> interfaceMap = new HashMap<>();
                        interfaceMap.put(p.getInterfaceKey(), p);
                        return interfaceMap;
                    },
                    (k1, k2) -> {
                        k1.putAll(k2);
                        return k1;
                    }));
        }

    }

    /**
     * 根据机构编码、机构类型和接口码值，查询配置URL
     *
     * @param orgCode 机构编码
     * @param orgType 机构类型
     * @param interfaceKey 接口码值
     * @return 机构银行URL
     */
    @Override
    public TOrgUrlManage getOrgUrlByOrgCodeAndKey(String orgCode, String orgType, String interfaceKey) {

        TOrgUrlManage tOrgUrlManage = null;

        //判断机构编号或银行编号是否为空
        if (BusiUtil.isNull(orgCode)) {
            throw ExceptionUtil.businessException("EE4002");
        }

        // 首先查询接口是否配置URL
        QueryWrapper<TOrgUrlManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ORG_CODE", orgCode).eq("ORG_TYPE", orgType).eq("INTERFACE_KEY", interfaceKey);
        tOrgUrlManage = this.getOne(queryWrapper);

        // 如果接口未配置URL，查询默认配置URL
        if (BusiUtil.isNull(tOrgUrlManage)) {
            QueryWrapper<TOrgUrlManage> commonWrapper = new QueryWrapper<>();
            commonWrapper.eq("ORG_CODE", orgCode).eq("ORG_TYPE", orgType).eq("INTERFACE_KEY", DEFAULT_INTERFACE_KEY);
            tOrgUrlManage = this.getOne(commonWrapper);
        }

        // 默认URL和接口URL都未配置，抛出异常
        if (BusiUtil.isNull(tOrgUrlManage)) {
            throw ExceptionUtil.businessException("EE4003", orgCode);
        }

        /*
        //接口码值为空默认COMMON
        interfaceKey = BusiUtil.isNull(interfaceKey)? DEFAULT_INTERFACE_KEY : interfaceKey;

        //优先从内存中获取
        Map<String, TOrgUrlManage> interfaceMap = OrgUrlMap.get(orgCode + orgType);
        if (BusiUtil.isNull(interfaceMap)) {
            tOrgUrlManage = null;
        } else {
            tOrgUrlManage = interfaceMap.get(interfaceKey);
        }

        //内存获取不到，再查数据库，并把查询到的数据保存到内存
        if (BusiUtil.isNull(tOrgUrlManage)) {
            QueryWrapper<TOrgUrlManage> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ORG_CODE", orgCode).eq("ORG_TYPE", orgType).eq("INTERFACE_KEY", interfaceKey);
            tOrgUrlManage = this.getOne(queryWrapper);
            //数据库也查不到，直接报错，查到维护到内存map中
            if (BusiUtil.isNull(tOrgUrlManage)) {
                throw ExceptionUtil.businessException("EE4003", orgCode);
            } else {
                Map<String, TOrgUrlManage> newMap = new HashMap<>();
                newMap.put(interfaceKey, tOrgUrlManage);
                OrgUrlMap.put(orgCode + orgType, newMap);
            }
        }
        */
        return tOrgUrlManage;
    }

    /**
     * 根据机构类型和接口码值，查询配置URL
     *
     * @param orgType 机构类型
     * @param interfaceKey 接口码值
     * @return 机构银行URL
     */
    @Override
    public TOrgUrlManage getOrgCodeByOrgTypeAndKey(String orgType, String interfaceKey) {
        QueryWrapper<TOrgUrlManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_type", orgType).eq("interface_key", interfaceKey);
        return this.getOne(queryWrapper);
    }

    /**
     * 根据机构编号，查询配置URL
     *
     * @param orgCode 机构编号
     * @return 机构银行URL
     */
    @Override
    public List<TOrgUrlManage> getPubCertByOrgCodeAndKey(String orgCode) {
        QueryWrapper<TOrgUrlManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_code", orgCode);
        queryWrapper.isNotNull("interface_key");
        return this.list(queryWrapper);
    }
}
